package com.lill.product.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lill.common.component.model.P;
import com.lill.common.component.util.IdUtil;
import com.lill.product.dao.IProductBaseDao;
import com.lill.product.dao.IProductClassifyDao;
import com.lill.product.model.ProductClassifyModel;
import com.lill.product.service.IProductClassifyService;

/**
 * Service实现类
 *
 * @author lill
 * @date 2021-08-27 09:14:44
 */
@Service("productClassifyService")
public class ProductClassifyServiceImpl implements IProductClassifyService {
	
    @Autowired
    private IProductClassifyDao productClassifyDao;
    @Autowired
    private IProductBaseDao productBaseDao;

    public ProductClassifyModel queryObject(String id) {
        return productClassifyDao.queryObject(id);
    }

    public List<ProductClassifyModel> queryList(Map<String, Object> map) {
        return productClassifyDao.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return productClassifyDao.queryTotal(map);
    }

    public int save(ProductClassifyModel productClassify) {
        productClassify.setId(IdUtil.createIdbyUUID());
        ProductClassifyModel parentClassify=productClassifyDao.queryObject(productClassify.getParentId());
        StringBuffer parentAll=new StringBuffer();
        if(StringUtils.isNotEmpty(parentClassify.getParentList())){
        	parentAll.append(",");
        }
        parentAll.append(parentClassify.getId());
        productClassify.setParentList(parentAll.toString());
        return productClassifyDao.save(productClassify);
    }

    public int update(ProductClassifyModel productClassify) {
        return productClassifyDao.update(productClassify);
    }

    
	public P deleteLogicByMap(Map<String, Object> map) {
		map.put("childIdList", map.get("ids"));
		int childNum=queryTotal(map);
		int productNum=productBaseDao.queryTotal(map);
		if((productNum+childNum)>0){
			return P.fail("含有子级分类或者分类有产品");
		}else{
			productClassifyDao.deleteLogicByMap(map);
			return P.ok();
		}
	}
}
